package com.zhj.structure.linear.utils;

/**
 * 一个节点
 * @PackageName:com.zhj.structure.utils
 * @ClassName:Node
 * @auter: 朱慧杰
 * @date:2020/8/8 0008 15:02
 **/
public class Node {

    // 节点内容
    private int data;
    // 下一个节点
    private Node next;

    public Node(int data) {
        this.data = data;
    }

    // 为节点追加节点
    public Node append(Node node) {
        Node currentNode = this;
        // 找到当前链表的最后一个节点
        while (true) {
            Node nextNode = currentNode.next;

            if (nextNode == null) {
                break;
            }
            currentNode = nextNode;
        }
        // 将值付给当前链表的最后一个节点
        currentNode.next = node;
        return this;
    }

    public void insertAfter(Node node) {
        // 取出下一个节点
        Node nextNext = this.next;
        this.next = node;
        this.next.next = nextNext;

    }

    public void show() {
        Node thisNode = this;
        Node currentNode = this;
        while (true) {
            System.out.print(currentNode.data + " \t");
            currentNode = currentNode.next;
            if (currentNode == thisNode) {
                break;
            }
        }
        System.out.println();
    }

    public void removeNext() {
        Node newNext = this.next.next;
        if(this.next.next == null) {
            this.next = null;
        }
        this.next = newNext;
    }

    // 获取下一个节点
    public Node next() {
        return this.next;
    }

    // 获取数据
    public int getData() {
        return this.data;
    }

    // 当前节点是否是最后一个节点
    public boolean isLast() {
        return next == null;
    }

}
